package com.tn.controller.cardrobot;

import cn.hutool.core.util.StrUtil;
import com.gitee.hifong45.FE;
import com.gitee.hifong45.Kv;
import com.gitee.hifong45.Ret;
import com.tn.controller.alarm.common.enums.CommonCodeEnum;
import com.tn.controller.alarm.common.model.CommonVO;
import com.tn.controller.alarm.common.model.cardrobot.CardSenderRobotInfoDO;
import com.tn.dao.external.subsystem.IGateTruckDao;
import com.tn.service.cardrobot.CardSendRobotService;
import com.tn.service.cardrobot.impl.CardSendRobotServiceImpl;
import com.tn.service.external.subsystem.impl.GateRestfulServiceImpl;
import com.tn.tcp.CardMachineHandle;
import com.tn.tcp.CardMachineInData;
import com.tn.utils.JsonUtil;
import com.tn.websocket.WebSocket;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName CardSendRobotController
 * @Description TODO
 * @Author Yezhongzhu
 * @Date 2022/1/10 15:12
 * @Version v1.0
 **/
@RestController
@RequestMapping("/aucy/cardrobot/")
@Slf4j
public class CardSendRobotController {
    @Autowired
    private CardSendRobotService cardSendRobotService;
    @Resource
    private CardSendRobotServiceImpl cardSendRobotServiceImpl;

    @Autowired
    private IGateTruckDao iGateDao;

    @Autowired
    private WebSocket webSocket;




    /**
     * 查询发卡机柜列表
     * @return
     */
    @RequestMapping("queryCardRobotList")
    @ResponseBody
    public CommonVO<List<CardSenderRobotInfoDO>> queryCardRobotList() {
        CommonVO<List<CardSenderRobotInfoDO>> commonVO = new CommonVO<>();
        try {
            List<CardSenderRobotInfoDO> cardSenderRobotInfoDOS = cardSendRobotService.queryCardRobotList();
            commonVO.setData(cardSenderRobotInfoDOS);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("queryCardRobotList error:{}",e.getMessage(),e);
        }
        return commonVO;
    }

    /**
     * 弹出所有集卡
     * @return
     */
    @Deprecated
    @RequestMapping("popupAllCard")
    @ResponseBody
    public CommonVO<Boolean> popupAllCard(int eventId,String stationId) {
        CommonVO<Boolean> commonVO = new CommonVO<>();
        try {
            cardSendRobotService.popupAllCard(eventId,stationId);
            commonVO.setData(true);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("popupAllCard error:{}",e.getMessage(),e);
        }
        return commonVO;
    }

    /**
     * 查询发卡机信息
     * @param stationId 发卡机ID
     * @param usable 是否只查询可用集卡. 无/空：查全部；all: 只查可用集卡
     * @return
     */
    @ApiOperation(value = "查询发卡模组实时信息")
    @GetMapping("/cardmachine")
    public Ret<Object> getCardMachineInfo(String stationId, String usable) {
        Kv ret = cardSendRobotServiceImpl.getCarMachineInfo(stationId, StrUtil.isBlank(usable));
        return Ret.Ok(ret);
    }


    /**
     * 清空Map
     * @return
     */
    @GetMapping("/clear-cardmachine")
    public Ret<Object> clearCardMachineInfo() {
        CardMachineInData.cardMachineMap=new HashMap<>();
        return Ret.Ok();
    }



    /**
     * 向发卡机发送弹卡指令
     * @param stationId 模组ID
     * @param slot 槽位
     * @return
     */
    @ApiOperation(value = "让发卡模组弹卡")
    @GetMapping("/popcard")
    public Ret<Object> popCard(String stationId, String slot, Integer all) {
        FE.nullThrow(stationId, "stationId"); // 必须指定发卡机
        if(all != null && all == 1) {
            CardMachineInData.showCMHeartBeatLog = true;
            log.info("使能心跳日志: {}", CardMachineInData.showCMHeartBeatLog);
            var ret = cardSendRobotServiceImpl.popAllCard(stationId);
            CardMachineInData.showCMHeartBeatLog = false;
            log.info("禁止心跳日志: {}", CardMachineInData.showCMHeartBeatLog);
            return Ret.Ok(ret);
        }
        String cardId = cardSendRobotServiceImpl.popCard(stationId, slot);
        String stationId1=null;
        String usable=null;
        Kv carMachineInfo = cardSendRobotServiceImpl.getCarMachineInfo(stationId1, StrUtil.isBlank(usable));
        webSocket.sendOneMessage("hs02",JsonUtil.obj2String(carMachineInfo));
        return Ret.Ok(Kv.of("stationId", stationId, "slot", slot, "cardId", cardId));
    }

    @GetMapping("/setShowCMDataLog")
    public Ret<Object> doLogCmInData(Integer showInDataLog, Integer showHeartBeatLog, Integer showAddOneCMLog) {
        if(showInDataLog != null) {
            CardMachineHandle.showCMInDataLog = showInDataLog > 0;
        }
        
        if(showHeartBeatLog != null) {
            CardMachineInData.showCMHeartBeatLog = showHeartBeatLog > 0;
        }
        if(showAddOneCMLog != null) {
            CardMachineInData.showCMAddOneCMLog = showAddOneCMLog > 0;
        }
        return Ret.Ok(Kv.of("showCMInDataLog", CardMachineHandle.showCMInDataLog,
                "showCMHeartBeatLog", CardMachineInData.showCMHeartBeatLog,
                "showCMAddOneCMLog", CardMachineInData.showCMAddOneCMLog)
        );
    }
}
